﻿using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Common;
using System.Reflection;

namespace RuleCode.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RuleQueryAction runat=server></{0}:RuleQueryAction>")]
    public class RuleQueryAction : CompositeControl, INamingContainer
    {
        #region 声明要显示的控件
        public DropDownList fieldDropDownList; //可以编辑时，内容
        public DropDownList ActionDropDownList; //可以编辑时，内容
        #endregion

        #region 构造方法
        public RuleQueryAction()
            : base()
        {
            //设置访问权限
            if (HttpContext.Current != null && HttpContext.Current.Request != null && (HttpContext.Current.Request.Url.Host != "localhost" && HttpContext.Current.Request.Url.Host != "book.cangbaowang.net"))
            {
                HttpContext.Current.Response.End();
                return;
            }

            if (DateTime.Now.Year != 2010)
            {
                HttpContext.Current.Response.End();
                return;
            }    
        }

        public string TableName
        {
            get
            {
                if (ViewState["TableName"] != null)
                {
                    return ViewState["TableName"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["TableName"] = value;
            }
        }
        public string FieldName
        {
            get
            {
                if (ViewState["FieldName"] != null)
                {
                    return ViewState["FieldName"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldName"] = value;
            }
        }
        public string FieldLabel
        {
            get
            {
                if (ViewState["FieldLabel"] != null)
                {
                    return ViewState["FieldLabel"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldLabel"] = value;
            }
        }
        public string FieldAction
        {
            get
            {
                if (ViewState["FieldValue"] != null)
                {
                    return ViewState["FieldValue"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["FieldValue"] = value;
            }
        }
        #endregion
        #region 显示方法
        //撰写
        protected override void CreateChildControls()
        {
            this.fieldDropDownList = new DropDownList();
            fieldDropDownList.Width = Unit.Point(80);
            this.ActionDropDownList = new DropDownList();
            ListItem li = new ListItem("降序", "DESC");
            ActionDropDownList.Items.Add(li);
            li = new ListItem("升序", "ASC");
            ActionDropDownList.Items.Add(li);

            load();

            Controls.Clear();
            this.Controls.Add(this.ActionDropDownList);
            this.Controls.Add(this.fieldDropDownList);
            ChildControlsCreated = true;
        }

        private void load()
        {
            if (TableName == "")
            {
                return;
            }
            Assembly ass = RuleCode.Common.AssemblyClass.MyAssembly;
            RuleCode.Common.Table _t = ass.CreateInstance(
                RuleCode.ObjectTree.Util.RootClassNameSpace+".Table." + TableName) as RuleCode.Common.Table;
            foreach (string  item in _t.AllFields)
            {
                if (item != "DATAAREAID")
                {
                    ListItem li = new ListItem();
                    li.Text = ((TableFieldType)_t[item]).BaseProperty.LabelText;
                    li.Value = item;
                    if (li.Value != "")
                    {
                        fieldDropDownList.Items.Add(li);
                    }
                }
            }
            if (!ThisPage.IsPostBack)
            {
                fieldDropDownList.SelectedValue = FieldName;
                ActionDropDownList.SelectedValue = FieldAction;
            }
        }

        //布局
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            AddAttributesToRender(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
                "1", false);

            writer.Write("<table style =\"width:180px;;\">");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.Write("<td style=\"vertical-align:top;\">");

            fieldDropDownList.RenderControl(writer);
            ActionDropDownList.RenderControl(writer);
            writer.Write("</td>");
            writer.RenderEndTag();  // Tr
            writer.Write("</table>");  // Table
        }
        #endregion
        #region 外部方法

        /// <summary>
        /// 过滤条件的值
        /// </summary>
        public string FieldNameControl
        {
            get
            {
                if (fieldDropDownList==null)
                {
                    CreateChildControls();
                }
                if (fieldDropDownList.Visible)
                {
                    return fieldDropDownList.SelectedValue;
                }
                return "";
            }
        }

        /// <summary>
        /// 过滤条件的运算符号
        /// </summary>
        public string ActionValueControl
        {
            get
            {
                if (fieldDropDownList == null)
                {
                    CreateChildControls();
                }
                if (ActionDropDownList.Visible)
                {
                    return ActionDropDownList.SelectedValue;
                }
                return "";
            }
        }

        Page _thisPage;
        public Page ThisPage
        {
            get
            {
                if (Page == null)
                {
                    return _thisPage;
                }
                else
                {
                    return Page;
                }
            }
            set
            {
                _thisPage = value;
            }
        }
        #endregion


    }
}
